package com.liwei.appio;

import java.io.*;

public class MyApp7 {
    public static void main(String[] args) {
        //缓存容量
        int bufferSize = 1024 * 1024 * 10;
        //源
        String pathSrc = "E:\\SZ-2208-(二)\\018-枚举\\001-java中的枚举对象.wmv";
        //目的地
        String pathDes = "D:\\aa\\aa.wmv";

        //目的地判定
        File file = new File(pathDes);
        //判断文件是否存在
        if (file.exists()) {
            //删除文件
            boolean delete = file.delete();
            if (!delete) {
                System.out.println("拷贝失败，文件无法删除");
                return;
            }
        }
        //创建文件
        try {
            boolean newFile = file.createNewFile();
            if (!newFile) {
                System.out.println("文件创建失败");
                return;
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("创建文件失败.....io异常");
            return;
        }
        //开始拷贝文件内容

        try {
            //读取源文件内容
            byte[] bytes = new byte[bufferSize];
            FileInputStream fileInputStream = new FileInputStream(pathSrc);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream,bufferSize);

            //开始准备连接目的地文件
            FileOutputStream fileOutputStream = new FileOutputStream(pathDes);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream,bufferSize);

            //计数器，记录拷贝了多少字节了
            int js = 0;
            int read = 0;
            //读bufferedInputStream.read(bytes, 0, bytes.length)
            while ((read = bufferedInputStream.read(bytes, 0, bytes.length)) != -1) {
                js += read;
                System.out.println("正在拷贝............." + js);
                //写
                bufferedOutputStream.write(bytes, 0, read);
            }

            bufferedOutputStream.flush();
            fileOutputStream.close();
            bufferedOutputStream.close();
            fileInputStream.close();
            bufferedInputStream.close();
            System.out.println("拷贝完毕");

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.out.println("读取源文件失败.........文件不存在");
            return;
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("读取源文件失败.........io错误");
            return;
        }
    }
}
